<template>
  <div id="md" class="py-3 px-2" v-html="content"></div>
</template>
<script>
export default {
  props: {
    content: {
      type: String,
      default: '',
    },
  },
  data() {
    return {
      titles: [],
    }
  },
  mounted() {
    this.parseContent()
    this.watchScroll()
  },
  methods: {
    parseContent() {
      const els = Array.prototype.slice.call(
        this.$el.querySelectorAll('h1,h2,h3,h4,h5,h6')
      )
      const html = document.documentElement
      this.titles = els.map((one) => {
        // 距离顶部的距离
        const { top } = one.getBoundingClientRect()
        // 滚动高度
        const scrollTop = html.scrollTop
        return {
          tagName: one.tagName.toLowerCase(), // 标签名
          text: one.innerText, // 文本内容
          contentEl: one, // 元素
          top: top + scrollTop, // 元素距离顶部的距离
        }
      })
      // this.$emit('onGetTitle', this.titles)
    },
    watchScroll() {
      const handleScroll = () => {
        const html = document.documentElement
        // 视口高度
        const height = html.offsetHeight
        // 滚动高度
        const scrollTop = html.scrollTop
        // 中线
        // const midLine = height / 2
        const midLine = height / 3
        // 筛选出来距离中线最近的两个
        let prevIndex, nextIndex
        for (let i = 0; i < this.titles.length; i++) {
          const one = this.titles[i]
          // 线上
          if (one.top - scrollTop <= midLine) {
            prevIndex = i
          }
          // 线下
          if (one.top - scrollTop > midLine) {
            nextIndex = i
          }

          if (prevIndex !== undefined && nextIndex !== undefined) {
            break
          }
        }
        // 中线上一个元素
        const prev = this.titles[prevIndex]
        // 中线下一个元素
        const next = this.titles[nextIndex]
        let activeIndex = prevIndex ?? 0
        // 判断，next存在，并且 next在视口内，并且next距离中线最近，则元素就选择next
        if (
          next &&
          next.top - scrollTop < height &&
          prev &&
          Math.abs(next.top - scrollTop - midLine) <
            Math.abs(prev.top - scrollTop - midLine)
        ) {
          activeIndex = nextIndex
        }
        this.$emit('onSelect', activeIndex)

        // 计算滚动百分比
        const maxScrollTop =
          document.querySelector('#__nuxt').offsetHeight - height
        const percent = Math.round((scrollTop / maxScrollTop) * 100)

        this.$emit('onScrollPercent', percent)
      }
      window.addEventListener('scroll', handleScroll)
      this.$on('hook:beforeDestory', () => {
        window.addEventListener('scroll', handleScroll)
      })
    },
  },
}
</script>
<style lang="scss" scoped>
.slide-card .avatar {
  width: 6.875rem;
  height: 6.875rem;
  margin: 1.25rem auto;
}
.slide-card .avatar img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}
.slide-card .auther {
  text-align: center;
  font-size: 1.5rem;
}
.slide-card .control {
  font-size: 1rem;
}
.slide-card .call-me {
  background-color: var(--btn-bg);
  color: var(--btn-color);
  line-height: 2;
  z-index: 1;
  cursor: pointer;
}
.slide-card .call-me::after {
  content: '';
  position: absolute;
  left: 0;
  top: 0;
  bottom: 0;
  right: 0;
  background: var(--btn-hover-color);
  z-index: -1;
  transition: transform 0.5s ease-out;
  -webkit-transform: scaleX(0);
  -moz-transform: scaleX(0);
  -o-transform: scaleX(0);
  -ms-transform: scaleX(0);
  transform: scaleX(0);
  -webkit-transform-origin: 0 50%;
  -moz-transform-origin: 0 50%;
  -o-transform-origin: 0 50%;
  -ms-transform-origin: 0 50%;
  transform-origin: 0 50%;
}
.slide-card .call-me:hover::after {
  -webkit-transform: scaleX(1);
  -moz-transform: scaleX(1);
  -o-transform: scaleX(1);
  -ms-transform: scaleX(1);
  transform: scaleX(1);
}
.slide-card .title {
  font-size: 1.125rem;
}
.slide-card .title i {
  font-size: 1.25rem;
}

.article-item-tip {
  height: 9.375rem;
}
.article-item-tip a {
  position: relative;
}
.article-item-tip a::after {
  content: '';
  position: absolute;
  left: 0;
  right: 0;
  top: 0;
  bottom: 0;
  background-color: rgba(0, 0, 0, 0.5);
  z-index: 1;
  transition: all 0.4s ease-in-out;
}
.article-item-tip a .cover-img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  transition: all 0.4s ease-in-out;
}
.post-title {
  position: absolute;
  left: 0;
  top: 50%;
  transform: translateY(-50%);
  padding: 1.875rem;
  color: var(--white);
  z-index: 2;
}
.article-item-tip:hover a::after {
  background-color: transparent;
}
.article-item-tip:hover a .cover-img {
  transform: scale(1.1);
}
.slide-md-title {
  max-height: 35vh;
  overflow-y: auto;
}
.slide-md-title li {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  cursor: pointer;
  border-left: 0.1875rem solid transparent;
  padding: 0 0.3125rem;
  border-top: 1px solid transparent;
  border-bottom: 1px solid transparent;
}
.slide-md-title li:hover {
  background-color: #f8f8f8;
}
[data-theme='dark'] .slide-md-title li:hover {
  background-color: #504f4f;
}
.slide-md-title li.active {
  border-left-color: #009d92;
  background: #00c4b6 !important;
  color: #fff;
}
.slide-md-title li.title-h2 {
  padding-left: 0.5rem;
}
.slide-md-title li.title-h3 {
  padding-left: 0.6875rem;
}
.slide-md-title li.title-h4 {
  padding-left: 0.875rem;
}
.slide-md-title li.title-h5 {
  padding-left: 1.0625rem;
}
.slide-md-title li.title-h6 {
  padding-left: 1.25rem;
}
.article-list li:hover .article-title {
  color: #007bff;
}
/* 设置滚动条的样式 */
.slide-md-title::-webkit-scrollbar {
  width: 6px;
}
/* 滚动槽 */
.slide-md-title::-webkit-scrollbar-track {
  -webkit-box-shadow: rgba(0, 0, 0, 0.05);
  border-radius: 6px;
}
/* 滚动条滑块 */
.slide-md-title::-webkit-scrollbar-thumb {
  border-radius: 6px;
  background: rgba(0, 0, 0, 0.03);
  -webkit-box-shadow: rgba(0, 0, 0, 0.1);
}
.slide-md-title::-webkit-scrollbar-thumb:window-inactive {
  background: rgba(0, 0, 0, 0.03);
}
.article-list li a {
  display: block;
}
.article-list li a img {
  width: 3.625rem;
  height: 3.625rem;
  object-fit: cover;
}
.article-list li a .post-message {
  width: calc(100% - 3.625rem);
  color: var(--font-color);
  line-height: 1.2;
  padding-left: 0.625rem;
}
.article-list li a .post-message .article-title {
  overflow: hidden;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  display: -webkit-box;
  transition: all 0.4s ease-in-out;
}
.article-list li a .post-message .article-time {
  color: #858585;
}
.slide-content .tag-a {
  color: #999;
  font-size: 1rem;
}
.slide-content .tag-a:hover {
  color: rgb(73, 177, 245);
}
.slide-content .tag-a.active {
  color: rgb(73, 177, 245);
  font-size: 1.4rem !important;
}
.file a {
  color: var(--font-color);
  text-decoration: none;
  line-height: 2.5;
  transition: all 0.4s ease-in-out;
  padding: 0 0.625rem;
}
.file li:hover a {
  background-color: var(--btn-bg);
  padding: 0 1.25rem;
}
.md-percentage {
  right: 0;
  top: 0;
  font-size: 2rem;
  line-height: 1;
  color: #00c4b6;
  font-style: italic;
}
.no-article {
  color: #17a2b8;
  font-size: 1.2rem;
}

.article-prev-next .article-item-tip.full {
  flex: initial !important;
  width: 100% !important;
  max-width: 100% !important;
}

@media (min-width: 992px) {
  .sticky-layout {
    position: -webkit-sticky;
    position: sticky;
    top: 4.0625rem;
  }
  .post-card .post-cover {
    width: 45%;
    height: 17.5rem;
  }
  .post-card .post-message {
    width: 55%;
  }
}
@media (max-width: 992px) {
  .slide-md {
    opacity: 0;
    position: fixed;
    bottom: 20px;
    right: 0;
    transform: translate(25%, 25%) scale(0);
    transform-origin: right bottom;
    /*transition: right .4s ease-in-out,
                    transform .4s ease-in-out,
                    opacity .4s ease-in-out;*/
  }
  .slide-md.show {
    z-index: 99;
    opacity: 1;
    right: 50%;
    transform: translate(50%) scale(1);
    width: 94%;
    max-width: 25rem;
  }
}
</style>

<style lang="scss">
:root {
  --blockquote-bg: rgba(73, 177, 245, 0.1);
  --blockquote-color: #6a737d;
  --card-bg: #fff;
}
#md {
  h1 {
    font-size: 26px;
    text-align: center;
    line-height: 2;
    font-weight: bold;
    margin-bottom: 10px;
  }
  h2 {
    font-size: 22px;
    line-height: 2;
    font-weight: bold;
    margin: 10px 0;
  }
  h3 {
    font-size: 20px;
    line-height: 2;
    font-weight: bold;
    margin: 10px 0;
  }
  h4 {
    font-size: 18px;
    line-height: 2;
    font-weight: bold;
    margin: 10px 0;
  }
  h5 {
    font-size: 16px;
    line-height: 2;
    font-weight: bold;
    margin: 10px 0;
  }
  h6 {
    font-size: 14px;
    line-height: 2;
    font-weight: bold;
    margin: 10px 0;
  }
}
#md p {
  margin-bottom: 1em;
}
#md p,
#md li {
  word-break: break-word;
}
#md img {
  max-width: 100%;
  display: block;
  margin: 1.5rem auto;
  border-radius: 0.3rem;
  cursor: pointer;
}
#md blockquote {
  margin: 0 0 1rem;
  padding: 0.3rem 0.8rem;
  border-left: 0.3rem solid #49b1f5;
  background-color: var(--blockquote-bg);
  color: var(--blockquote-color);
  border-radius: 0.3rem;
}
#md blockquote p:last-child {
  margin: 0;
}
#md a {
  color: #49b1f5;
}
#md table {
  display: block;
  width: 100%;
  max-width: 100%;
  overflow: auto;
  box-sizing: border-box;
  word-break: keep-all;
}
#md table thead,
#md table thead {
  width: 100%;
}
#md table tr {
  width: 100%;
  border-top: 1px solid #c6cbd1;
  background-color: var(--card-bg);
}
#md table tr:nth-child(2n) {
  background-color: #f6f8fa;
}
[data-theme='dark'] #md table tr:nth-child(2n) {
  background-color: #343a40;
}
#md table tr td,
#md table tr th {
  padding: 6px 13px;
  border: 1px solid #dfe2e5;
}
#md pre {
  display: block;
  font-size: 87.5%;
  background-color: #212529;
  padding: 0.5rem;
  border-radius: 0.3rem;
}
#md pre code {
  word-break: keep-all;
  font-size: 13px;
  color: #fff;
}
#md code {
  color: #e83e8c;
  word-wrap: break-word;
}
</style>
